import {Component, OnInit, EventEmitter, ViewChild} from '@angular/core';
import {flyIn} from 'app/comm-components/animations/fly-in';
import {MainMenuService} from '../lib/service/main-menu.service';
import {NavigationEnd, Router} from '@angular/router';
import {MainHeaderComponent} from '../comm-components/main-header/main-header.component';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss'],
  animations: [
    flyIn
  ],
  providers: [
    MainMenuService
  ]
})
export class MainComponent implements OnInit {
  hasHeaderAdditional = true;
  hasAside = true;

  @ViewChild(MainHeaderComponent) mainHeader: MainHeaderComponent;

  constructor(
    private mainMenuService: MainMenuService,
    private router: Router,
  ) {
    this.mainMenuService.eventbus.subscribe(_event => {
        if (_event.type === 1) {
          this.hasHeaderAdditional = !(
            _event.url === '/main/index'
          );
          this.hasAside = !(
           _event.url === '/main/index'
          );
          this.mainHeader.getAuthStatus();
        }
      }
    );
  }

  ngOnInit() {
    this.hasHeaderAdditional = !(
      this.router.url === '/main/index'
    );
    this.hasAside = !(
     this.router.url === '/main/index'
    );
    this.router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        if (event.urlAfterRedirects) {
          this.mainMenuService.eventbus.emit({
            type: 1,
            url: event.urlAfterRedirects
          });
        } else {
          this.mainMenuService.eventbus.emit({
            type: 1,
            url: event.urlAfterRedirects
          });
        }
      }
    });
  }


}
